import utils from './utils.js'
import moment from "moment";
let base={};
base.install = function (Vue, options) {

    //重设表头
    Vue.prototype.resetTableHead = function (tableHead){
        let _this=this;
        for (let tableHeadKey in tableHead.rows) {
            tableHead.rows[tableHeadKey].index=parseInt(tableHeadKey);//设置排序值
        }
        //存储默认值 HEAD_DEFAULT_
        let storage=window.localStorage;
        storage.setItem("HEAD_DEFAULT_"+tableHead.code,JSON.stringify(tableHead));

        //获取用户设置的值 HEAD_
        let storageTableHead=storage.getItem("HEAD_"+tableHead.code);
        if (storageTableHead!=null) {
            let headData = JSON.parse(storageTableHead);
            tableHead = headData;
        }else{
            _this.$http.post('/tableHeader/getTableHeader/'+tableHead.code).then(function(res){
                if(res.data.success){
                    if (res.data.data!=null){//服务器如果有配置，则读取服务器的配置
                        tableHead=res.data.data;
                    }else{
                        console.log("服务器无此表头的数据");
                    }
                }else{
                    _this.$message.error('操作错误！错误编码:'+res.data.code+'错误原因:'+res.data.msg);
                }
            },function(){
                console.error("请求数据错误");
            });
        }
        utils.initTableHeadFormat(tableHead);
        //initTableHeadFormat(tableHead);
    };
    //重设表头
    Vue.prototype.resetTableHeight = function (tableRef){
        let dataTableHeight = window.innerHeight - tableRef.$el.offsetTop - 50;
        return dataTableHeight;
    }

    Vue.prototype.floatFilter = function (value){
        if (typeof(value)=="number"){
            let realVal = parseFloat(value).toFixed(2);
            return realVal;
        }
        return null;
    }
    //格式化日期
    Vue.prototype.formatAmt = function (row, column,cellValue) {
        if (typeof(cellValue)=="number"){
            let realVal = parseFloat(cellValue).toFixed(2);
            return realVal;
        }
        return cellValue;
    }
    //格式化日期
    Vue.prototype.formatDate = function (row, column,cellValue) {
        if (cellValue!=null && cellValue>999){
            let timeStr= moment(cellValue).format('YYYY-MM-DD HH:mm:ss');
            return timeStr;
        }else if (cellValue===-62135798400000){
            return '';
        }else{
            return cellValue;
        }
    }
    Vue.prototype.filterDate = function (cellValue) {
        if (cellValue!=null && cellValue>999){
            let timeStr= moment(cellValue).format('YYYY-MM-DD HH:mm:ss');
            return timeStr;
        }else{
            return cellValue;
        }
    }
    //深度拷贝
    Vue.prototype.deepCopy = function (json) {
        let _json=JSON.parse(JSON.stringify(json));
        return _json;
    }

    Vue.prototype.byteToString = function(limit){
        let size = "";
        if( limit < 0.1 * 1024 ){ //如果小于0.1KB转化成B
            size = limit.toFixed(2) + "B";
        }else if(limit < 0.1 * 1024 * 1024 ){//如果小于0.1MB转化成KB
            size = (limit / 1024).toFixed(2) + "KB";
        }else if(limit < 0.1 * 1024 * 1024 * 1024){ //如果小于0.1GB转化成MB
            size = (limit / (1024 * 1024)).toFixed(2) + "MB";
        }else{ //其他转化成GB
            size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        }

        let sizeStr = size + "";
        let len = sizeStr.indexOf("\\.");
        let dec = sizeStr.substr(len + 1, 2);
        if(dec == "00"){//当小数点后为00时 去掉小数部分
            return sizeStr.substring(0,len) + sizeStr.substr(len + 3,2);
        }
        return sizeStr;
    }


};



export default base;

